<?php

namespace app\common\model\mysql;

use app\common\business\DataAuth;
use think\Model;

class SysDictData extends Model
{
    /**
     * 自动生成写入时间
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 新增前
     * @param $model
     * @return mixed|void
     */
    protected static function onBeforeInsert($model){
        $userData = (new DataAuth())->getUserDetails();
        $model->create_by = $userData['userName'];
    }

    /**
     * 更新前
     * @param $model
     * @return mixed|void
     */
    protected static function onBeforeUpdate($model)
    {
        $userData = (new DataAuth())->getUserDetails();
        $model->update_by = $userData['userName'];
    }

    //查询字典数据表数据
    public function getDictList($dictType){
        if(empty($dictType)){
            return false;
        }
        $where = [
            "dict_type" =>$dictType,
        ];
        $result = $this->where($where)->select();
        return $result;
    }

    /**
     * 查询字典类型数据列表
     * @param $where
     * @param $field
     * @param $limit
     * @param $page
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getNormalDictDataList($where, $field,$limit,$page){
        $order = [
            "dict_sort" =>"asc",
        ];
        $result = $this->where($where)->field($field)->order($order)
            ->paginate([
                'list_rows' => $limit,
                'page' => $page,
            ]);
        return $result;
    }

    /**
     * 根据$dict_type,$dict_label去数据库查询是否存在这条记录
     * @param $dict_type
     * @param $dict_label
     * @return SysDictData|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDictDataByNameModel($dict_type,$dict_label,$dict_code){
        if(empty($dict_type) || empty($dict_label)){
            return false;
        }
        $where = [
            "dict_type" =>$dict_type,
            "dict_label"=>$dict_label
        ];
        if($dict_code){
            $result = $this->where("dict_code", "<>", $dict_code)->where($where)->find();
        }else{
            $result = $this->where($where)->find();
        }
        return $result;
    }

    /**
     * 根据$dict_type,$dict_value去数据库查询是否存在这条记录
     * @param $dict_type
     * @param $dict_value
     * @return SysDictData|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDictDataByValueModel($dict_type,$dict_value,$dict_code){
        if(empty($dict_type) || empty($dict_value)){
            return false;
        }
        $where = [
            "dict_type" =>$dict_type,
            "dict_value"=>$dict_value
        ];
        if($dict_code){
            $result = $this->where("dict_code", "<>", $dict_code)->where($where)->find();
        }else{
            $result = $this->where($where)->find();
        }
        return $result;
    }

    /**
     * 根据id获取字典类型数据信息
     * @param $id
     * @return SysDictData|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDictByDataId($id){
        if(empty($id)){
            return false;
        }
        $where = [
            "dict_code" =>$id,
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    /**
     * 删除字典类型数据信息
     * @param $ids
     * @return bool
     */
    public function DeleteDictDataIds($ids){
        if(empty($ids)){
            return false;
        }
        $where = [
            ['dict_code', 'in', $ids]
        ];
        $result = $this->where($where)->delete();
        return $result;
    }

    /**
     * 根据$dict_types去数据库查询是否存在这条记录
     * @param $dict_type
     * @return SysDictData|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDictDataByDataModel($dict_type){
        if(empty($dict_type)){
            return false;
        }
        $where = [
            ['dict_type', 'in', $dict_type]
        ];
        $result = $this->where($where)->find();
        return $result;
    }
}
